package com.action.gulimall.product.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

//import org.apache.shiro.authz.annotation.RequiresPermissions;
import com.action.common.utils.PageUtils;
import com.action.common.utils.R;
import com.action.gulimall.product.entity.AttrEntity;
import com.action.gulimall.product.service.AttrAttrgroupRelationService;
import com.action.gulimall.product.service.AttrService;
import com.action.gulimall.product.service.CategoryService;
import com.action.gulimall.product.vo.AttrGroupRelationVo;
import com.action.gulimall.product.vo.AttrGroupWithAttrsVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.action.gulimall.product.entity.AttrGroupEntity;
import com.action.gulimall.product.service.AttrGroupService;


/**
 * 属性分组
 *
 * @author shilun
 * @email sunlightcs@gmail.com
 * @date 2021-06-08 18:22:04
 */
@RestController
@RequestMapping("product/attrgroup")
public class AttrGroupController {
    @Autowired
    private AttrGroupService attrGroupService;
    @Autowired
    private CategoryService categoryService;
    @Autowired
    private AttrService attrService;
    @Autowired
    private AttrAttrgroupRelationService attrAttrgroupRelationService;

    /**
     * 列表
     */
    @RequestMapping("/list/{catelogId}")
    //@RequiresPermissions("product:attrgroup:list")
    public R list(@RequestParam Map<String, Object> params,@PathVariable(name="catelogId") Long catelogId){
        //PageUtils page = attrGroupService.queryPage(params);
        PageUtils page = attrGroupService.queryPage(params,catelogId);
        return R.ok().put("page", page);
    }

    ///product/attrgroup/{attrgroupId}/attr/relation
    @GetMapping(value = "/{attrgroupId}/attr/relation")
    public R attrRelation(@PathVariable("attrgroupId") Long attrgroupId) {

        List<AttrEntity> entities = attrService.getRelationAttr(attrgroupId);

        return R.ok().put("data",entities);
    }

    /* * @Author sl
     * @Description 获取分类下所有分组&关联属性
     * @Date 23:21 2021/6/14/014
     * @Param [catelogId]
     * @return com.action.common.utils.R
     **/
    @GetMapping(value = "/{catelogId}/withattr")
    public R getAllGroupAttrByCatelogId(@PathVariable("catelogId") Long catelogId) {

        List<AttrGroupWithAttrsVo> attrGroupWithAttrsVos = attrGroupService.getAllGroupAttrByCatelogId(catelogId);

        return R.ok().put("data",attrGroupWithAttrsVos);
    }
    
    
    /**
     * 获取属性分组没有关联的其他属性
     */
    @GetMapping(value = "/{attrGroupId}/noattr/relation")
    public R attrNoattrRelation(@RequestParam Map<String, Object> params,
                                @PathVariable("attrGroupId") Long attrGroupId) {
        PageUtils page = attrService.getNoRelationAttr(params,attrGroupId);
        return R.ok().put("page",page);
    }

    /**
     * 信息
     */
    @RequestMapping("/info/{attrGroupId}")
    //@RequiresPermissions("product:attrgroup:info")
    public R info(@PathVariable("attrGroupId") Long attrGroupId){
		AttrGroupEntity attrGroup = attrGroupService.getById(attrGroupId);
        Long[] paths = categoryService.getCatelogPath(attrGroup.getCatelogId());
        attrGroup.setCatelogPath(paths);
        return R.ok().put("attrGroup", attrGroup);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    //@RequiresPermissions("product:attrgroup:save")
    public R save(@RequestBody AttrGroupEntity attrGroup){
		attrGroupService.save(attrGroup);

        return R.ok();
    }
    /* * @Author sl
     * @Description 添加属性与分组关联关系
     * @Date 21:26 2021/6/14/014
     * @Param [vos]
     * @return com.action.common.utils.R
     **/
    ///product/attrgroup/attr/relation
    @PostMapping(value = "/attr/relation")
    public R addRelation(@RequestBody List<AttrGroupRelationVo> vos) {

        attrAttrgroupRelationService.saveBatch(vos);

        return R.ok();

    }
    /**
     * 修改
     */
    @RequestMapping("/update")
    //@RequiresPermissions("product:attrgroup:update")
    public R update(@RequestBody AttrGroupEntity attrGroup){
		attrGroupService.updateById(attrGroup);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    //@RequiresPermissions("product:attrgroup:delete")
    public R delete(@RequestBody Long[] attrGroupIds){
		//attrGroupService.removeByIds(Arrays.asList(attrGroupIds));
        attrGroupService.removeGroupAndRelation(Arrays.asList(attrGroupIds));
        return R.ok();
    }

    ///product/attrgroup/attr/relation/delete
    @RequestMapping("/attr/relation/delete")
    //@RequiresPermissions("product:attrgroup:delete")
    public R deleteRelation(@RequestBody AttrGroupRelationVo[] attrGroupRelationVos){
        //attrGroupService.removeByIds(Arrays.asList(attrGroupIds));
        attrAttrgroupRelationService.removeRelations(attrGroupRelationVos);
        return R.ok();
    }
}
